package com.banshou.installs.ui.activity;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;

import com.banshou.installs.R;
import com.banshou.installs.databinding.NearCommBinding;
import com.banshou.installs.model.dao.ResponseXQ;
import com.banshou.installs.model.net.Observer;
import com.banshou.installs.model.net.ResponseInfo;
import com.banshou.installs.model.net.RetrofitManager2;
import com.banshou.installs.model.net.RxUtil;
import com.banshou.installs.ui.adapter.QXAdapter;
import com.banshou.installs.utils.DialogUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by cjq on 2018/4/19.
 * Email: stoic_yb@139.com
 * features:
 */
public class XQActivity extends BaseActivity {
    private NearCommBinding mBinding;
    private QXAdapter mAdapter;
    private List<ResponseXQ.BuildList> xqList;
    private DialogUtils dialogUtils;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.near_comm);
        initView();
    }

    private void initView() {
        xqList = new ArrayList<>();
        dialogUtils = new DialogUtils();
        mBinding.title.pagerTitle.setText("选择小区");
        mBinding.title.back.setOnClickListener(view -> finish());
        mAdapter = new QXAdapter(this, R.layout.nearcomm_item, xqList);
        mBinding.recycleview.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        mBinding.recycleview.setAdapter(mAdapter);
        getList();
        getId();
    }

    private void getList() {
        RetrofitManager2.getInstance()
                .create(ResponseInfo.class)
                .getXQ()
                .compose(RxUtil.lifecycle(this))
                .subscribe(new Observer<ResponseXQ>() {
                    @Override
                    protected void onNextStep(ResponseXQ responseXQ) {
                        dialogUtils.hideAni();
                        xqList.clear();
                        xqList.addAll(responseXQ.getCommunity_list());
                        mAdapter.notifyDataSetChanged();
                    }

                    @Override
                    protected void onErrorResult() {
                        dialogUtils.hideAni();
                    }
                });
    }

    private void getId() {
        mAdapter.setOnItemOnClickListener((Id, name,type) -> {
            Intent intent = new Intent();
            intent.putExtra("XQID", Id);
            intent.putExtra("XQTYPE", type);
            intent.putExtra("XQNAME",name);
            setResult(1, intent);
            finish();
        });
    }

}
